package com.meiyu.live.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.meiyu.live.R;
import com.meiyu.live.popj.ActiveInfo;
import com.meiyu.live.ui.mine.EventActivity;

/**
 * Created by LI on 2016/12/28.
 */

public class VinDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private String imgUrl;
    private String remark;

    public VinDialog(Context context, String imgUrl, String remark) {
        super(context,R.style.vin_dialog);
        this.context = context;
        this.imgUrl = imgUrl;
        this.remark = remark;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_vin, null);
        setContentView(view);

        ImageView iv_vin = (ImageView) view.findViewById(R.id.iv_vin);
        ImageView iv_del = (ImageView) view.findViewById(R.id.iv_del);

        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();
        imageLoader.displayImage(imgUrl, iv_vin, options);

        iv_vin.setOnClickListener(this);
        iv_del.setOnClickListener(this);

//        Window dialogWindow = getWindow();
//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
//        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
//        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_vin:
                if (!TextUtils.isEmpty(remark)) {
                    ActiveInfo activeInfo = new ActiveInfo("活动", imgUrl, remark);
                    EventActivity.start(context, activeInfo);
                }
                break;
            case R.id.iv_del:
                dismiss();
                break;
        }
    }
}
